<template>
  <v-dialog
    :title="title"
    :close-on-click-modal="false"
    v-model="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  >
        <el-col :span="12">
    
          
           <el-form-item label="年度" prop="annual"
                :rules="[{required:true,message:'项目年份不能为空',trigger:'blur'}                  ]">
               <el-date-picker
                v-model="inputForm.annual"
                type="year"
                placeholder="请选择年度"              
                style="width:100%"     />
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="指标类型" prop="indicatorType"
                :rules="[
                 ]">
                <el-select v-model="inputForm.indicatorType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('indicator_type ')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="指标金额(元)" prop="indicatorAmount"
                :rules="[
                 ]">
              <el-input v-model="inputForm.indicatorAmount" placeholder="请填写指标金额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="集团内金额(元)" prop="intraGroupAmount"
                :rules="[
                 ]">
              <el-input v-model="inputForm.intraGroupAmount" placeholder="请填写集团内金额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="集团外金额(元)" prop="extraGroupAmount"
                :rules="[
                 ]">
              <el-input v-model="inputForm.extraGroupAmount" placeholder="请填写集团外金额"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="集团内剩余" prop="intraGroupRemainder"
                :rules="[
                 ]">
              <el-input v-model="inputForm.intraGroupRemainder" placeholder="请填写集团内剩余"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="集团外剩余" prop="extraGroupRemainder"
                :rules="[
                 ]">
              <el-input v-model="inputForm.extraGroupRemainder" placeholder="请填写集团外剩余"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="状态" prop="status"
                :rules="[
                 ]">
                    <el-radio-group v-model="inputForm.status">
                        <el-radio v-for="item in $dictUtils.getDictList('status_type')" :value="item.value" :key="item.value">{{item.label}}</el-radio>
                    </el-radio-group>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注" prop="remarks"
                :rules="[
                 ]">
              <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注"     ></el-input>
           </el-form-item>
        </el-col>
    <el-col :span="24">
        <el-tabs v-model="indicatorTab">
            <el-tab-pane label="指标分配表">
                  <el-button @click="addIndicatorSplitRow" type="primary">新增</el-button>
                  <el-table
                  class="table"
                  size="small"
                  :data="inputForm.indicatorSplitDTOList.filter((item) => { return item.delFlag !== '1'})"
                  style="width: 100%">
                  <el-table-column
                    prop="office.name"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="所属组织">
                      <template  #default="{row}">
                         <office-select v-model="row.office.id"/>
                      </template>
                  </el-table-column>
                  <el-table-column
                    prop=""
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="指标金额(元)">
                      <template  #default="{row}">
                          <el-input v-model="row.indicatorAmount" placeholder="请填写指标金额"     ></el-input>
                      </template>
                  </el-table-column>
                  <el-table-column
                    prop=""
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="集团内金额(元)">
                      <template  #default="{row}">
                          <el-input v-model="row.intraGroupAmount" placeholder="请填写集团内金额"     ></el-input>
                      </template>
                  </el-table-column>
                  <el-table-column
                    prop=""
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="集团外金额(元)">
                      <template  #default="{row}">
                          <el-input v-model="row.extraGroupAmount" placeholder="请填写集团外金额"     ></el-input>
                      </template>
                  </el-table-column>
                  <el-table-column
                    prop=""
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="备注">
                      <template  #default="{row}">
                         <el-input type="textarea" v-model="row.remarks" placeholder="请填写备注"     ></el-input>
                      </template>
                  </el-table-column>
                  <el-table-column
                    fixed="right"
                    label="操作"
                    width="100">
                    <template #default="{row}">
                      <el-button  @click="delIndicatorSplitRow(row)" type="text" size="small">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
        </el-tabs>
       </el-col>
     </el-row>
    </el-form>
    <template #footer>    
        <span class="dialog-footer">
          <el-button @click="visible = false" icon="circle-close">关闭</el-button>
          <el-button  type="primary" v-if="method != 'view'" @click="doSubmit()" icon="circle-check" v-noMoreClick>确定</el-button>
        </span>
    </template>    
  </v-dialog>
</template>

<script>
  import OfficeSelect from '@/components/officeSelect'
  import indicatorService from '@/api/indicator/indicatorService'

import moment from "moment";
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        indicatorTab: '0',
        inputForm: {
          id: '',
          indicatorSplitDTOList: [],
          annual: '',
          indicatorType: '',
          indicatorAmount: '',
          intraGroupAmount: '',
          extraGroupAmount: '',
          intraGroupRemainder: '',
          extraGroupRemainder: '',
          status: '',
          remarks: ''
        }
      }
    },
    components: {
      OfficeSelect
    },
    created () {
    },
    methods: {
      init (method, id) {
        this.method = method
        if (method === 'add') {
          this.title = `新建指标`
        } else if (method === 'edit') {
          this.title = '修改指标'
        } else if (method === 'view') {
          this.title = '查看指标'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = id
          this.indicatorTab = '0'
          this.inputForm.indicatorSplitDTOList = []
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            indicatorService.queryById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)

              this.inputForm.annual =this.inputForm.annual.toString()
              this.loading = false
            })
          }
        })
      },
      saveIndicatorSplitRow (child) {
        if (child[0] === '') {
          this.inputForm.indicatorSplitDTOList.push(child[1])
        } else {
          this.inputForm.indicatorSplitDTOList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.indicatorSplitDTOList.splice(index, 1, child[1])
            }
          })
        }
      },
      addIndicatorSplitRow () {
        this.inputForm.indicatorSplitDTOList.push({
          office: {
            id: ''
          },
          indicatorAmount: '',
          intraGroupAmount: '',
          extraGroupAmount: '',
          remarks: ''
        })
      },
      delIndicatorSplitRow (child) {
        this.inputForm.indicatorSplitDTOList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.indicatorSplitDTOList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.indicatorSplitDTOList.splice(index, 1, item)
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true;
              this.inputForm.annual =moment(this.inputForm.annual).format("YYYY")
            indicatorService.save(this.inputForm).then((data) => {
              this.visible = false
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>

  
